<?php
/**
 * functions.php
 *
 * @copyright 2018 opencart.cn
 *
 * All Rights Reserved
 * @link http://guangdawangluo.com
 *
 * @author StifferChen <chenlin@opencart.cn>
 * @created 2018-04-26 11:51
 * @modified 2018-04-26 11:51
 */

if (!function_exists('is_debug')) {
    function is_debug()
    {
        return defined('DEBUG') && DEBUG;
    }
}

if (!function_exists('debug_bar')) {
    function debug_bar()
    {
        return Registry::getInstance()->get('debug_bar');
    }
}

/**
 * get installed extension
 */
if (!function_exists('checkExtensions')) {
    function checkExtensions($extension)
    {
        $loaded_extensions = get_loaded_extensions();
        return in_array($extension, $loaded_extensions);
    }
}

if (!function_exists('checkFileType')) {
    function checkFileType($tmp_name = '', $type = array())
    {
        if (!$tmp_name || !$type) {
            return false;
        }
        $fileInfo = new finfo(FILEINFO_MIME_TYPE);
        $fileType = $fileInfo->file($tmp_name);
        return in_array($fileType, $type);
    }
}

if (!function_exists('checkPath')) {
    function checkPath($path)
    {
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
            @chmod($path, 0777);
        }
    }
}

if (!function_exists('resizeImage')) {
    function resizeImage($image, $width = 100, $height = 100)
    {
        Registry::getInstance()->get('load')->model('tool/image');
        if (is_file(DIR_IMAGE . $image)) {
            return Registry::getInstance()->get('model_tool_image')->resize($image, $width, $height);
        }
        return Registry::getInstance()->get('model_tool_image')->resize('no_image.png', $width, $height);
    }
}

if (!function_exists('registry')) {
    function registry($params = '')
    {
        if ($params) {
            return Registry::getInstance()->get(strtolower($params));
        }
        return Registry::getInstance();
    }
}

if (!function_exists('uriLink')) {
    function uriLink($link = '', $params = '', $ssl = 'ssl')
    {
        if (!$link) {
            return '';
        }
        return Registry::getInstance()->get('url')->link($link, $params, $ssl);
    }
}

if (!function_exists('d')) {
    function d(...$args)
    {
        foreach ($args as $x) {
            (new \Illuminate\Support\Debug\Dumper())->dump($x);
        }
    }
}

if (!function_exists('dd')) {
    function dd(...$args)
    {
        d($args);
        die();
    }
}

if (!function_exists('get')) {
    function get($data)
    {
        return isset(Registry::getInstance()->get('request')->get[$data]) ? Registry::getInstance()->get('request')->get[$data] : null;
    }
}

if (!function_exists('post')) {
    function post($data)
    {
        return isset(Registry::getInstance()->get('request')->post[$data]) ? Registry::getInstance()->get('request')->post[$data] : null;
    }
}

if (!function_exists('trans')) {
    function trans($text)
    {
        return Registry::getInstance()->get('language')->get($text);
    }
}

if (!function_exists('setTitle')) {
    function setTitle($title = '')
    {
        Registry::getInstance()->get('document')->setTitle($title);
    }
}

if (!function_exists('formatCurrency')) {
    function formatCurrency($price, $code = '')
    {
        if (!$code) {
            return Registry::getInstance()->get('currency')->format($price, Registry::getInstance()->get('session')->data['currency']);
        }
        return Registry::getInstance()->get('currency')->format($price, $code);
    }
}

if (!function_exists('breadcrumb')) {
    function breadcrumb($data = array())
    {
        $html = '<ul class="breadcrumb">';
        $html .= '<li><a href="' . uriLink('common/home') . '"><i class="fa fa-home" title="' . trans('text_home') . '"></i></a></li>';
        if ($data) {
            foreach ($data[0] as $item) {
                $html .= '<li><a href="' . $item['href'] . '">' . $item['text'] . '</a></li>';
            }
        }
        $html .= "</ul>";
        return $html;
    }
}

if (!function_exists('redirect')) {
    function redirect($route)
    {
        Registry::getInstance()->get('response')->redirect($route);
    }
}

if (!function_exists('setTitle')) {
    function setTitle($title)
    {
        Registry::getInstance()->get('document')->setTitle($title);
    }
}

if (!function_exists('addScript')) {
    function addScript($args)
    {
        if (!$args) {
            return;
        }

        if (is_array($args)) {
            foreach ($args as $arg) {
                Registry::getInstance()->get('document')->addScript($arg);
            }
        }

        if (is_string($args)) {
            Registry::getInstance()->get('document')->addScript($args);
        }
    }
}

if (!function_exists('addStyle')) {
    function addStyle($args)
    {
        if ($args) {
            return;
        }
        if (is_array($args)) {
            foreach ($args as $arg) {
                Registry::getInstance()->get('document')->addStyle($arg);
            }
        }

        if (is_string($args)) {
            Registry::getInstance()->get('document')->addStyle($args);
        }
    }
}

if (!function_exists('config')) {
    function config($key)
    {
        return Registry::getInstance()->get('config')->get($key);
    }
}

if (!function_exists('template')) {
    function template($route)
    {
        $route = str_replace('.', '/', $route[0]);
        if (is_file(DIR_TEMPLATE . config('config_theme') . '/template/' . $route . '.twig')) {
            return config('config_theme') . '/template/' . $route . '.twig';
        }
        return 'default/template/' . $route . '.twig';
    }
}

if (!function_exists('session_get')) {
    function session_get($key)
    {
        return isset(Registry::getInstance()->get('session')->data[$key]) ? Registry::getInstance()->get('session')->data[$key] : null;
    }
}

if (!function_exists('session_set')) {
    function session_set($key, $value)
    {
        Registry::getInstance()->get('session')->data[$key] = $value;
    }
}

if (!function_exists('device')) {
    function device()
    {
        return new Mobile_Detect();
    }
}

if (!function_exists('is_mobile')) {
    function is_mobile()
    {
        return device()->isMobile();
    }
}

if (!function_exists('is_pad')) {
    function is_pad()
    {
        return device()->isTablet();
    }
}

if (!function_exists('view_product')) {
    function view_product($result)
    {
        $image = resizeImage($result['image'], Registry::getInstance()->get('config')->get('theme_' . registry('config')->get('config_theme') . '_image_product_width'), Registry::getInstance()->get('config')->get('theme_' . registry('config')->get('config_theme') . '_image_product_height'));

        if (registry('customer')->isLogged() || !registry('config')->get('config_customer_price')) {
            $price = registry('currency')->format(registry('tax')->calculate($result['price'], $result['tax_class_id'], registry('config')->get('config_tax')), Registry::getInstance()->get('session')->data['currency']);
        } else {
            $price = false;
        }

        if ((float)$result['special']) {
            $special = registry('currency')->format(registry('tax')->calculate($result['special'], $result['tax_class_id'], registry('config')->get('config_tax')), Registry::getInstance()->get('session')->data['currency']);
        } else {
            $special = false;
        }

        if (registry('config')->get('config_tax')) {
            $tax = registry('currency')->format((float)$result['special'] ? $result['special'] : $result['price'], Registry::getInstance()->get('session')->data['currency']);
        } else {
            $tax = false;
        }

        if (registry('config')->get('config_review_status')) {
            $rating = (int)$result['rating'];
        } else {
            $rating = false;
        }

        return array(
            'image' => $image,
            'price' => $price,
            'special' => $special,
            'tax' => $tax,
            'rating' => $rating,
            'name' => $result['name']
        );
    }
}